<template>
  <van-row>
    <van-nav-bar
      title="供应商详情"
      left-arrow
      left-text="返回"
      @click-left="$emit('close')"
    >
      <template #right>
        <van-popover
            v-model="showPopover"
            placement="bottom-end"
            trigger="click"
            :actions="actions"
            @select="handleAction"
        >
          <template #reference>
            <van-icon name="ellipsis" size="20" />
          </template>
        </van-popover>
      </template>
    </van-nav-bar>
    <van-cell title="供应商编号" :value="supplierInfo.supplierNo" />
    <van-cell title="供应商名称" :value="supplierInfo.supplierName" />
    <van-cell title="联系人" :value="supplierInfo.contacts" />
    <van-cell title="手机" :value="supplierInfo.phoneNum" />
    <van-cell title="传真" :value="supplierInfo.fax" />
    <van-cell title="邮箱" :value="supplierInfo.email" />
    <van-cell title="地址" :value="supplierInfo.address" />
    <van-cell title="备注" :value="supplierInfo.remark" />
    <van-popup v-model="showSubmit" position="right" :style="{ width: '100%', height: '100%' }">
      <supplier-submit-module ref="submitModule" @close="handleClose" />
    </van-popup>
  </van-row>
</template>

<script>
import { deleteSupplier, selectSupplierById } from '@/api/supplier'
import { Dialog } from 'vant'
import SupplierSubmitModule from '@/views/supplier/supplierSubmit.vue'

export default {
  name: 'SupplierDetailModule',
  components: { SupplierSubmitModule },
  data() {
    return {
      showPopover: false,
      showSubmit: false,
      title: '',
      supplierInfo: {},
      actions: [
        { icon: 'edit', text: '修改', action: 'edit', disabled: false },
        { icon: 'delete-o', text: '删除', action: 'delete', disabled: false }]
    }
  },
  methods: {
    init(id) {
      selectSupplierById(id).then(resp => {
        if (resp.success) {
          this.supplierInfo = resp.data
        }
      })
    },
    handleAction(row) {
      if (row.action === 'delete') {
        this.doDelete()
      } else if (row.action === 'edit') {
        this.toEdit()
      }
    },
    toEdit() {
      this.showSubmit = true
      this.$nextTick(() => {
        this.$refs.submitModule.init(this.supplierInfo.id)
      })
    },
    doDelete() {
      Dialog.confirm({
        title: '删除确认',
        message: '是否删除此信息'
      }).then(() => {
        deleteSupplier({ ids: [this.supplierInfo.id] }).then(resp => {
          if (resp.success) {
            this.$toast.success('删除成功')
            this.$emit('close')
          }
        })
      }).catch(() => {})
    },
    handleClose() {
      this.showSubmit = false
      this.init(this.supplierInfo.id)
    }
  }
}
</script>

<style scoped>

</style>
